<%@ page import="java.sql.*" %>  
<%@ page import="java.text.SimpleDateFormat" %>  
<%@ page import="java.util.concurrent.TimeUnit" %>  
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<%@ page import="java.util.Date" %>  

 
  
<html>  
<head>  
    <title>用户退房结算</title>  
</head>  
<body>  
<h1>用户退房结算</h1>  
  
<%  

Class.forName("org.mariadb.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mariadb://10.220.140.102:3366/studb11","stua","zHZUA65r");

    // 从请求中获取用户名和密码
    String inputUsername = request.getParameter("name");
    String inputPassword = request.getParameter("phone");
    // 验证用户身份
    String userSql = "SELECT * FROM 成都梦之旅文君楼庭院酒店（宽窄巷子店） WHERE name='"+inputUsername+"' AND phone='"+inputPassword+"'";
    PreparedStatement userStmt = conn.prepareStatement(userSql);
    ResultSet rs = userStmt.executeQuery(); 
    
    String checkInDateStr = request.getParameter("indate");  
    String checkOutDateStr = request.getParameter("outdate");   
    String priceStr = request.getParameter("price"); // 或者其他获取 priceStr 的方式  
    
    double roomPrice = 0.0;  
      
    if (priceStr != null && !priceStr.trim().isEmpty()) {  
        try {  
            roomPrice = Double.parseDouble(priceStr.trim()); // 使用 trim() 去除前后空格  
        } catch (NumberFormatException e) {  
            out.println("房间价格格式错误，请确保输入有效的数字。");  
            // 可能需要在这里退出或重定向到其他页面  
        }  
    } else {  
        out.println("房间价格未提供或格式不正确，请确保输入有效的数字。");  
        // 在这里处理未提供价格的情况  
    }  
    
  
    // 假设用户验证和退房操作都成功了  
    if (rs.next()) {  
    	String roomSql = "UPDATE 成都梦之旅文君楼庭院酒店（宽窄巷子店） SET status = '已退房' WHERE name='"+inputUsername+"'";
        Statement roomStmt = conn.createStatement();
        int rowsAffected = roomStmt.executeUpdate(roomSql); 
  
        if (rowsAffected > 0) {
        	 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
             Date indate = sdf.parse(checkInDateStr);  
             Date outdate = sdf.parse(checkOutDateStr);  
             long diffInMilliseconds = outdate.getTime() - indate.getTime();  
             long diffInDays = TimeUnit.DAYS.convert(diffInMilliseconds, TimeUnit.MILLISECONDS);  
             double totalCost = diffInDays * roomPrice;  
             out.println("退房和结算成功，感谢您的入住！总费用：" + totalCost + " 元");    
        } else {
            out.println("登录成功，但当前没有房间可退。");
        }
    } else {
        out.println("登录失败，用户名或密码错误。");
    }
    
        
         
  
    // 关闭资源
    rs.close();
    userStmt.close();
    conn.close();
%>  
  
<form method="post">  
    <!-- 其他表单字段 -->  
  
    <label for="username">用户名:</label>  
    <input type="text" id="username" name="username" required><br><br>  
    <label for="password">密码:</label>  
    <input type="password" id="password" name="password" required><br><br>  
  
    <label for="price">房间价格:</label>  
    <input type="text" id="price" name="price" required><br><br>  
  
    <input type="submit" value="退房结算">  
</form>  
  
<!-- 其他HTML代码 -->  
  
</body>  
</html>
